package com.zaoke.web.dao.food.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;

import com.zaoke.web.bo.food.FoodPromotion;
import com.zaoke.web.dao.JdbcSupport;
import com.zaoke.web.dao.food.FoodPromotionDao;
import com.zaoke.web.utils.DoubleFormat;

public class FoodPromotionDaoImpl extends JdbcSupport implements
		FoodPromotionDao {

	public List<FoodPromotion> getAllActivePromotionsByDates(Date start,
			Date end, Integer[] states) {
		String sql = "select `id`, `promotion_date`, `food_id`, `single_margin`, `combo_margin`, `state`, `add_userid`, `add_time`, `lastmod_userid`,`lastmod_time` from `zaoke`.`promotion` where `state` in (";
		sql += StringUtils.join(states, ",");
		sql += ") and `promotion_date` between ? and ?";

		return getTemplateMgr().getReadTp().query(sql,
				new RowMapper<FoodPromotion>() {

					public FoodPromotion mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						FoodPromotion promotion = new FoodPromotion();
						promotion.setId(rs.getLong("id"));
						promotion.setPromotionDate(rs.getDate("promotion_date"));
						promotion.setFoodid(rs.getLong("food_id"));
						promotion.setSingleMargin(rs.getDouble("single_margin"));
						promotion.setComboMargin(rs.getDouble("combo_margin"));
						promotion.setState(rs.getInt("state"));
						promotion.setAddUserid(rs.getLong("add_userid"));
						promotion.setAddTime(rs.getDate("add_time"));
						promotion.setLastmodUserid(rs.getLong("lastmod_userid"));
						promotion.setLastmodTime(rs.getDate("lastmod_time"));
						return promotion;
					}
				}, start, end);
	}

	@Override
	public boolean deletePromotionByFoodId(Long foodid) {
		String sql = "delete from `zaoke`.`promotion` where food_id=?";
		return getTemplateMgr().getWriteTp().update(sql,
				new Object[] { foodid }) > 0;
	}

	@Override
	public boolean newFoodPromotion(FoodPromotion promotion,Long userid) {
		if (promotion == null)
			return false;
		String sql = "insert into `zaoke`.`promotion`";
		sql += "(promotion_date,food_id,single_margin,combo_margin,state,add_userid,add_time,lastmod_userid,lastmod_time)";
		sql += " values(?,?,?,?,?,?,now(),?,now())";
		Long foodid = promotion.getFoodid();
		Double combo = promotion.getComboMargin();
		Date date = promotion.getPromotionDate();
		Double single = promotion.getSingleMargin();
		
		Object[] params = new Object[]{date,foodid,DoubleFormat.format(single),DoubleFormat.format(combo),FoodPromotion.STATE_ACTIVE,userid,userid};
		return getTemplateMgr().getWriteTp().update(sql, params)>0;
	}
	
	@Override
	public boolean updateFoodPromotion(FoodPromotion promotion,Long userid) {
		if (promotion == null)
			return false;
		String sql = "update `zaoke`.`promotion` set lastmod_time=now() ";
		Long id = promotion.getId();
		Long foodid = promotion.getFoodid();
		Double combo = promotion.getComboMargin();
		Date date = promotion.getPromotionDate();
		Double single = promotion.getSingleMargin();
		
		List<Object> params = new LinkedList<Object>();
		
		if(foodid!=null)
		{
			sql += " , food_id=?";
			params.add(foodid);
		}
		if(combo!=null)
		{
			sql += " , combo_margin=?";
			params.add(DoubleFormat.format(combo));
		}
		if(single!=null)
		{
			sql += " , single_margin=?";
			params.add(DoubleFormat.format(single));
		}
		if(date!=null)
		{
			sql += " ,promotion_date=?";
			params.add(date);
		}
		sql += " where id=?";
		params.add(id);
		return getTemplateMgr().getWriteTp().update(sql, params.toArray())>0;
	}

	@Override
	public List<FoodPromotion> getAllActivePromotions(Integer[] states) {
		String sql = "select `id`, `promotion_date`, `food_id`, `single_margin`, `combo_margin`, `state`, `add_userid`, `add_time`, `lastmod_userid`,`lastmod_time` from `zaoke`.`promotion` where `state` in (";
		sql += StringUtils.join(states, ",");
		sql += ")";

		return getTemplateMgr().getReadTp().query(sql,
				new RowMapper<FoodPromotion>() {

					public FoodPromotion mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						FoodPromotion promotion = new FoodPromotion();
						promotion.setId(rs.getLong("id"));
						promotion.setPromotionDate(rs.getDate("promotion_date"));
						promotion.setFoodid(rs.getLong("food_id"));
						promotion.setSingleMargin(rs.getDouble("single_margin"));
						promotion.setComboMargin(rs.getDouble("combo_margin"));
						promotion.setState(rs.getInt("state"));
						promotion.setAddUserid(rs.getLong("add_userid"));
						promotion.setAddTime(rs.getDate("add_time"));
						promotion.setLastmodUserid(rs.getLong("lastmod_userid"));
						promotion.setLastmodTime(rs.getDate("lastmod_time"));
						return promotion;
					}
				});
	}

	@Override
	public FoodPromotion getPromotionById(Long id) {
		String sql = "select `id`, `promotion_date`, `food_id`, `single_margin`, `combo_margin`, `state`, `add_userid`, `add_time`, `lastmod_userid`,`lastmod_time` from `zaoke`.`promotion` where `id`=? ";
		List<FoodPromotion> promotions = getTemplateMgr().getReadTp().query(sql,
				new RowMapper<FoodPromotion>() {

			public FoodPromotion mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				FoodPromotion promotion = new FoodPromotion();
				promotion.setId(rs.getLong("id"));
				promotion.setPromotionDate(rs.getDate("promotion_date"));
				promotion.setFoodid(rs.getLong("food_id"));
				promotion.setSingleMargin(rs.getDouble("single_margin"));
				promotion.setComboMargin(rs.getDouble("combo_margin"));
				promotion.setState(rs.getInt("state"));
				promotion.setAddUserid(rs.getLong("add_userid"));
				promotion.setAddTime(rs.getDate("add_time"));
				promotion.setLastmodUserid(rs.getLong("lastmod_userid"));
				promotion.setLastmodTime(rs.getDate("lastmod_time"));
				return promotion;
			}
		},id);
		
		if(promotions!=null && promotions.size()>0)
			return promotions.get(0);
		return null;
	}

	@Override
	public boolean deletePromotionById(Long id) {
		String sql = "delete from `zaoke`.`promotion` where id=?";
		return getTemplateMgr().getWriteTp().update(sql,
				new Object[] { id }) > 0;
	}
}
